package com.lzm.flash;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;


public class MainActivity extends AppCompatActivity implements MainContactPage.IContactPageInterface{

    MainContactPage m_mainContactPage;
    MainPersonalCenterPage m_mainPersonalCenterPage;

    MainHandler m_myHeandler;
    RadioGroup m_bottomRadioButtonGroup=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_bottomRadioButtonGroup=(RadioGroup)findViewById(R.id.bottom_group);
        //主界面headler
        m_myHeandler= new MainHandler(this.getMainLooper());

        //接受广播线程
        BroadcastRecvThread thr = new BroadcastRecvThread(m_myHeandler);
        new Thread(thr).start();

        initFragments();

        initButtonListener();
    }

    private void initButtonListener()
    {
        m_bottomRadioButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.personal_center_button){
                    getFragmentManager().beginTransaction()
                            .hide(m_mainContactPage)
                            .show(m_mainPersonalCenterPage)
                            .commitAllowingStateLoss();
                }
                else if(i==R.id.contact_button)
                {
                    getFragmentManager().beginTransaction()
                            .hide(m_mainPersonalCenterPage)
                            .show(m_mainContactPage)
                            .commitAllowingStateLoss();
                }
            }
        });
    }


    private void initFragments()
    {
        m_mainContactPage=MainContactPage.newInstance();
        m_mainPersonalCenterPage=MainPersonalCenterPage.newInstance();

        getFragmentManager().beginTransaction()
                .add(R.id.frame_layout_main,m_mainContactPage)
                .add(R.id.frame_layout_main,m_mainPersonalCenterPage)
                .commitAllowingStateLoss();
        getFragmentManager().beginTransaction()
                .hide(m_mainPersonalCenterPage).commitAllowingStateLoss();

    }



    public  class MainHandler extends Handler {
        public MainHandler(){}
        public MainHandler(Looper L){
            super(L);
        }

        @Override
        public void handleMessage(Message msg) {
            Bundle b=msg.getData();
            switch (b.getInt("msg_type"))
            {
                case Type.recv_broadcast:
                    Bundle data= b.getBundle("data");
                    m_mainContactPage.updataNumberListData(data);
                    break;
                case Type.recv_broadcast_err:

                    break;
            }
        }
    }
}
